package base_grammar

import (
	"bytes"
	"fmt"
	"strings"
)

func string_f1() {
	var s1 string = "s1"
	var s2 = "s2"
	s3 := "s3"
	s4 := `
		line 1
		line 2
		line 3
	`
	fmt.Printf("s1: %v\n", s1)
	fmt.Printf("s2: %v\n", s2)
	fmt.Printf("s3: %v\n", s3)
	fmt.Printf("s4: %v\n", s4)

	// 字符串连接
	msg := s1 + s2
	fmt.Printf("msg: %v\n", msg)

	msg2 := fmt.Sprintf("%s, %s", s1, s2)
	fmt.Printf("msg2: %v\n", msg2)

	msg3 := strings.Join([]string{s3, s2}, ",")
	fmt.Printf("msg3: %v\n", msg3)

	// 缓冲区  效率高
	var buffer bytes.Buffer
	buffer.WriteString("tom")
	buffer.WriteString(" and")
	buffer.WriteString(" jerry")
	fmt.Printf("buffer.String(): %v\n", buffer.String())

	// 字符串切片操作

	hello := "hello worLd!"
	a := 2
	fmt.Printf("hello[a]: %v\n", hello[a])
	fmt.Printf("hello[a]: %c\n", hello[a])
	b := 5
	fmt.Printf("hello[a]: %v\n", hello[a:b])

	fmt.Printf("hello[a]: %v\n", hello[:b])
	fmt.Printf("hello[a]: %v\n", hello[a:])

	fmt.Printf("len(hello): %v\n", len(hello))
	fmt.Printf("strings.Split(\" \"): %v\n", strings.Split(hello, " "))
	fmt.Printf("strings.Contains(hello, \"hello\"): %v\n", strings.Contains(hello, "hello"))
	fmt.Printf("strings.ToLower(hello): %v\n", strings.ToLower(hello))
	fmt.Printf("strings.ToUpper(hello): %v\n", strings.ToUpper(hello))
	fmt.Printf("strings.HasPrefix(hello, \"hello\"): %v\n", strings.HasPrefix(hello, "hello"))
	fmt.Printf("strings.HasSuffix(hello, \"!\"): %v\n", strings.HasSuffix(hello, "!"))

	fmt.Printf("strings.Index(hello, \"ll\"): %v\n", strings.Index(hello, "ll"))
	fmt.Printf("strings.LastIndex(hello, \"l\"): %v\n", strings.LastIndex(hello, "l"))
}
